<?php

/**
 * Entry Point ?
 *  
 * Okay:
 * 
 * Take into consideration 
 * A). Multiple Website 
 * B). URL Rewrite
 * C). Multi-Language
 * D). SEO
 * 
 * E). For Small Requests, Can we by-pass some over-head ?
 * F). Locale Settings ( money, time formatting )
 * 
 * G). 
 * 
 */
/**
 * Algorithm
 * 
 * 1. GET TOCKEN
 * 2. GET REGISTRY VALUE
 * 3. AUTHENTICATION
 * 4. DISPATCH 
 * 5. PROCESS
 * 6. SEND OUTPUT
 * 
 * 
 */
/**
 * @todo: Learn about unit testing and documentation
 */
/**
 *
 * GET REGISTRY 
 * 
 * registry is 1.
 * 1 is very lower level registry
 * 1 has to output a static logo
 * 
 * 
 * 
 * step1.
 * ======
 * create registry object
 * assign registry id
 * 
 * process registry
 * 
 * 
 * step2. How to process registry
 * =======
 * 
 * many ways....
 * 
 * they have function .
 * 
 * return '<img src="" />';
 * 
 * what if we have multiple registry
 * 
 * step3. Aggregator
 * =======
 * 
 * 
 * 
 * Creating a website : is creating a registry.
 * creating a page is also a registry value.
 * 
 * 
 * page : is entity ? no idea..
 * 
 * page : 
 * 
 * ==
 * 
 * how to handle forms  and events 
 * 
 * -- its a big thing..
 * 
 * for that, 
 * 
 * 
 * 
 * 
 * 
 * 
 */
error_reporting(0);
define('_P', dirname(__FILE__) . "/");
define('_U', isset($_SERVER['HTTPS']) ? 'https://' : 'http://' . $_SERVER['HTTP_HOST'] . "/");

define('STORAGE_HOST', 'localhost');
define('STORAGE_USER', 'root');
define('STORAGE_PASS', 'hp');
define('STORAGE_DB', 'eventdriven');

function __autoload($class) {
    include_once _P . "core/{$class}.class.php";
}

function q($query) {
    $s = storage::__s();
    return $s->format_data($s->query($query));
}
function qg($query,$groupBy) {
    $s = storage::__s();
    return $s->format_data($s->query($query),$groupBy);
}
function qd($query,$aux='') {
    d($query);
    $s = storage::__s();
    return $s->format_data($s->query($query));
}

function d($arr) {
    if (is_array($arr) || is_object($arr)) {
        print '<pre style="width:90%;padding:0.5em;color:red;background-color:#F7F9BD;font-family:verdana;font-size:10pt;margin-top:10px">';
        print_r($arr);
        print "</pre>";
    } else {
        print '<div style="width:90%;padding:0.5em;color:red;background-color:#F7F9BD;font-family:verdana;font-size:10pt;margin-top:10px">Debug:<br>' . $arr . '</div>';
    }
}

function _reg_back_trace() {
    $debug_data = debug_backtrace();
    array_shift($debug_data);
    $output = 'Execution Backtrace:<br><ul>';
    foreach ($debug_data as $debug_data) {
        if ($debug_data['function'] != 'handleShutdown')
            $output .= "<li><b> " . $debug_data['file'] . "</b> [ Line : <b>" . $debug_data['line'] . "</b> ] [ Function : " . $debug_data['function'] . " ]<br></li>";
    }
    $output .= '</ul>';
    d($output);
}

/**
 * General function to handle print/output.
 * Based upon registry parameters.
 * 
 * For example, a registry may want to send output in json, in that case:
 * message should be printed in json format.
 * or, if registry is related to operation, than, may be we want message 
 * to Fail or Success.
 * 
 * These kind of meta information is specified in registry
 * 
 * @param string $message Message to print
 * 
 */
function _reg_print($message) {

    /**
     * @todo implement output according to registry metadata
     */
//   switch(registry_output_format){
//       
//   } 

    print $message;
}

function _ex_h($exception) {
    $traceline = "#%s %s(%s): %s(%s)";
    $trace = $exception->getTrace();
    foreach ($trace as $key => $stackPoint) {
        $trace[$key]['args'] = array_map('gettype', $trace[$key]['args']);
    }

    $result = array();
    foreach ($trace as $key => $stackPoint) {
        if (!empty($stackPoint['file'])) {
            $result[] = sprintf(
                    $traceline, $key, $stackPoint['file'], $stackPoint['line'], $stackPoint['function'], implode(', ', $stackPoint['args'])
            );
        }
    }
    $result[] = '#' . ++$key . ' {main}';



    $class = get_class($exception);
    $error = $exception->getMessage();
    $file = $exception->getFile();
    $line = $exception->getLine();
    $result = implode("<br />", $result);


    $message = <<<_

   <div style="width:90%;padding:0.5em;color:red;background-color:#EDEDED;font-family:verdana;font-size:10pt;margin-top:10px">
        <ul>
            <li>$error</li>
            <li>$file [Line : $line]</li>
            <li>$class</li>
            <li>$result</li>
        </ul>
   </div>
   
_;
    _reg_print($message);
}

function _er_h($no, $str, $file, $line, $context) {

    /**
     * @todo response should be printed according to Registry > Output Handler
     */
    $message = <<<_

   <div style="width:90%;padding:0.5em;color:red;background-color:#F7F9BD;font-family:verdana;font-size:10pt;margin-top:10px;">
        <ul>
            <li><b>$str</b></li>
            <li><b>$file</b> [ Line : <b>$line</b> ]</li>
        </ul>
   </div>
   
_;
    _reg_print($message);
    _reg_back_trace();
    die;
}

set_error_handler("_er_h");
set_exception_handler('_ex_h');

setlocale(LC_ALL, 'en_US');
date_default_timezone_set('Asia/Calcutta');


register_shutdown_function('handleShutdown');

function handleShutdown() {
    $error = error_get_last();
    if ($error !== NULL) {
        $info = "[SHUTDOWN] file:" . $error['file'] . " | ln:" . $error['line'] . " | msg:" . $error['message'] . PHP_EOL;
        _er_h(-1, $error['message'], $error['file'], $error['line'], '');
    } else {
        //_er_h("SHUTDOWN");
    }
}

$lang = lang::__l();

$lang->set(1, 'hello');

# now registry
# get registry
# process registry
# get request
# how do you get the registry ???
# it can be a long process... 
# may be..url or some params...we prefer a url...
# 

$reg = new registry('3');
$reg->process();

out::__s()->doOut();

        


/**
 *  see, we have few types of operational request....
 * 
 * some thing asking to add things into database.... application/add/registry
 * application/edit/data
 * appliation/edit/registry 
 * hmmmm.....
 * add new registries would be easier ?
 * 
 * may be yes, may be not...
 * 
 * any application we take...
 * 
 * whether its api integration or web service request...
 * 
 * how we can automise it ?
 * 
 * a complete request to render a website
 * an ajax request
 * 
 * a consequences of action...
 * 
 * login registry have many options
 * 
 *   
 * 
 */
/*
print json_encode(array(
    'name'=>'loginFrm',
    'isajax'=>'no',
    'type'=>'interactForm',
    'controls'=>'text,passowrd,submit'
));
*/
